<script setup lang="ts">
import { getCategoryTopAPI } from '@/services/category'
import { getHomeBannerAPI } from '@/services/home'
import type { CategoryTopItem } from '@/types/category'
import type { BannerItem } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref, computed } from "vue"
import PageSkeleton from './components/PageSkeleton.vue'

// 存储轮播图数据
const bannerList = ref<BannerItem[]>([])
// 存储分类列表数据
const categoryList = ref<CategoryTopItem[]>([])
// 存储列表高亮下标
const activeIndex = ref(0)
// 控制骨架屏是否加载状态
const isFinish = ref(false)

// 获取轮播图数据
const getBannerData = async () => {
    const res = await getHomeBannerAPI(2)
    bannerList.value = res.result
}

// 获取分类列表数据
const getCategoryTopData = async () => {
    const res = await getCategoryTopAPI()
    categoryList.value = res.result
}

// 提取二级分类数据(可能出现undefined, 使用?符号)
const subCategoryList = computed(() => {
    return categoryList.value[activeIndex.value]?.children || []
})

// 组件挂载后调用(获取一二级分类数据、骨架屏业务)
onLoad(async () => {
    await Promise.all([getBannerData(), getCategoryTopData()])
    isFinish.value = true
})
</script>

<template>
    <view class="viewport" v-if="isFinish">
        <!-- 搜索框 -->
        <view class="search">
            <view class="input">
                <text class="icon-search">女靴</text>
            </view>
        </view>
        <!-- 分类 -->
        <view class="categories">
            <!-- 左侧：一级分类 -->
            <scroll-view class="primary" scroll-y>
                <view v-for="(item, index) in categoryList" :key="item.id" :class="{ active: index === activeIndex }"
                    @tap="activeIndex = index" class="item">
                    <text class="name"> {{ item.name }} </text>
                </view>
            </scroll-view>
            <!-- 右侧：二级分类 -->
            <scroll-view class="secondary" scroll-y>
                <!-- 焦点图 -->
                <XtxSwiper class="banner" :list="bannerList" />
                <!-- 内容区域 -->
                <view class="panel" v-for="item in subCategoryList" :key="item.id">
                    <view class="title">
                        <text class="name">{{ item.name }}</text>
                        <navigator class="more" hover-class="none">全部</navigator>
                    </view>
                    <view class="section">
                        <navigator v-for="goods in item.goods" :key="goods.id" class="goods" hover-class="none"
                            :url="`/pages/goods/goods?id=${goods.id}`">
                            <image class="image" :src="goods.picture"></image>
                            <view class="name ellipsis">{{ goods.name }}</view>
                            <view class="price">
                                <text class="symbol">¥</text>
                                <text class="number">{{ goods.price }}</text>
                            </view>
                        </navigator>
                    </view>
                </view>
            </scroll-view>
        </view>
    </view>
    <PageSkeleton v-else />
</template>

<style lang="scss">
/* H5端默认开启scoped样式隔离, 导致组件基础样式无效 */
@import './components/styles/category.scss';
</style>